#include "CollisionSystem.hpp"
#include <GQE/Entity/classes/Instance.hpp>
#include "GameState.hpp"
CollisionSystem::CollisionSystem(GQE::IApp& theApp,GameState* theGameState):
  GQE::ICollisionSystem(theApp),
  mGameState(theGameState),
  mWarpMap(""),
  mWarp(false),
  mWarpPosition(0,0)
{
}

CollisionSystem::~CollisionSystem()
{
}

void CollisionSystem::AddProperties(GQE::IEntity* theEntity)
{
  GQE::ICollisionSystem::AddProperties(theEntity);
  theEntity->mProperties.Add<bool>("bWall",false);
  theEntity->mProperties.Add<bool>("bWater",false);
  theEntity->mProperties.Add<bool>("bShadow",false);
  theEntity->mProperties.Add<bool>("bWarp",false);
  theEntity->mProperties.Add<GQE::Uint32>("uDamage",0);
  theEntity->mProperties.Add<bool>("bItem",false);
}


void CollisionSystem::EntityCollision(GQE::IEntity* theMovingEntity,GQE::IEntity* theOtherEntity, GQE::typeCollisionData theCollisionData)
{
  // Don't check if its us
  if(theMovingEntity->GetID() != theOtherEntity->GetID())
  {
    sf::IntRect anIntersectRect;
    if(theOtherEntity->mProperties.Get<bool>("bShadow")==true)
    {
      theOtherEntity->mProperties.Set<bool>("bVisible",true);
    }
    if(theOtherEntity->mProperties.Get<bool>("bWall"))
    {
      sf::Vector2f anPositionA=theMovingEntity->mProperties.Get<sf::Vector2f>("vPosition");
      sf::Vector2f anPositionB=theOtherEntity->mProperties.Get<sf::Vector2f>("vPosition");
#if SFML_VERSION_MAJOR < 2
      if(anMovingData.Side==GQE::COL_LEFT)
      {
        anPositionA.x += theCollisionData.IntersectRect.GetWidth();
      }
      if(anMovingData.Side==GQE::COL_RIGHT)
      {
        anPositionA.x -= theCollisionData.IntersectRect.GetWidth();
      }
      if(anMovingData.Side==GQE::COL_TOP)
      {
        anPositionA.y += theCollisionData.IntersectRect.GetHeight();
      }
      if(anMovingData.Side==GQE::COL_BOTTOM)
      {
        anPositionA.y -= theCollisionData.IntersectRect.GetHeight();
      }
#else
	  if(theCollisionData.IntersectRect.width<theCollisionData.IntersectRect.height)
		{
			if(anPositionA.x>=anPositionB.x)
			{
				anPositionA.x += theCollisionData.IntersectRect.width;
			}
			else 
			{
				anPositionA.x -= theCollisionData.IntersectRect.width;
			} 
		}
		if(theCollisionData.IntersectRect.width>theCollisionData.IntersectRect.height)
		{
			if(anPositionA.y>=anPositionB.y)
			{
				anPositionA.y += theCollisionData.IntersectRect.height;
			}
			else 
			{
				anPositionA.y -= theCollisionData.IntersectRect.height;
			} 
		}

#endif
      theMovingEntity->mProperties.Set<sf::Vector2f>("vPosition",anPositionA);
    }
    if(theOtherEntity->mProperties.Get<bool>("bShadow") &&
        theMovingEntity->mProperties.Get<bool>("bPlayer"))
    {
      theOtherEntity->mProperties.Set<bool>("bVisible",false);
    }

            
    if(theMovingEntity->mProperties.Get<bool>("bPlayer"))
    {
			if(theOtherEntity->mProperties.Get<bool>("bWarp"))
			{
        mWarp=true;
        mWarpMap=theOtherEntity->mProperties.Get<std::string>("sMapFilename");
        mWarpPosition=theOtherEntity->mProperties.Get<sf::Vector2f>("vDestPosition");
			}
		}
  } // if(anEntityA->GetID() != anEntityB->GetID())
  if(mWarp)
  {
    mGameState->mPlayer->mProperties.Set<sf::Vector2f>("vPosition",mWarpPosition);
	  mGameState->LoadMap(mWarpMap);
    mWarp=false;
  } // for(anIterA = mMovables.begin(); anIterA != mMovables.end(); ++anIterA)
}

